var app = getApp();
import*as API from "../../utils/config.js";
Page({
  data: {
    currentTab: 0,
    winHeight: "",
    juvenileList:null,
    youngList:null,
    maidenList:null,
    beautifulList:null
  },
  onLoad: function (options) {
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth; 
        var calc = clientHeight * rpxR-88;
        // console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    //ajax首次加载数据
    //app.ajaxRequire()

    if(!wx.getStorageSync('juvenileList')){
      this.ajaxRequire('少年漫画','juvenileList')
      wx.showLoading({
        title: '加载中...',
      })
    }else{
      wx.getStorage({
        key: 'juvenileList',
        success: function(res) {
            that.setData({
              juvenileList:res.data
            })
        } 
      })
    };
  },
  onshow() {
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current  
    });
    if(e.detail.current==1 && !wx.getStorageSync('youngList')){
      this.ajaxRequire('青年漫画','youngList')
      wx.showLoading({
        title: '加载中...',
      })
    }else{
      wx.getStorage({
        key: 'youngList',
        success: function(res) {
            that.setData({
              youngList:res.data
            })
        } 
      })
    }; 
    if(e.detail.current==2 && !wx.getStorageSync('maidenList')){
      this.ajaxRequire('少女漫画','maidenList')
      wx.showLoading({
        title: '加载中...',
      })
    }else{
      wx.getStorage({
        key: 'maidenList',
        success: function(res) {
            that.setData({
              maidenList:res.data
              
            })
        } 
      })
    };
    if(e.detail.current==3 && !wx.getStorageSync('beautifulList')){
      this.ajaxRequire('耽美漫画','beautifulList')
      wx.showLoading({
        title: '加载中...',
      })
    }else{
      wx.getStorage({
        key: 'beautifulList',
        success: function(res) {
            that.setData({
              beautifulList:res.data
              
            })
        } 
      })
    }
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  // ajax请求
  ajaxRequire(type_name,list_data) {
    var _this=this;
    wx.request({
      url: API.book,
      data: {
        type: type_name
      },
      success: (res) => {
       // console.log(res.data.data.data.result.bookList)
       wx.setStorageSync(list_data,res.data.data.result.bookList)
       wx.setStorage({
        key:list_data,
        data:res.data.data.result.bookList
      })
       wx.hideLoading()
        _this.setData({
          [list_data]:res.data.data.result.bookList
        })
      }
    })
  },

})